/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gen2phen.labs.actions;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.gen2phen.labs.services.Freebase;
import org.json.simple.parser.ParseException;

/**
 *
 * @author pedrolopes
 *
 * GEN2PHEN Semantic Web Labs: Knowledge Network
 *
 */
@UrlBinding("/freebase/{$event}/{query}")
public class FreebaseActionBean implements ActionBean {

    private ActionBeanContext context;
    private String query;   // Freebase Search query

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public ActionBeanContext getContext() {
        return context;
    }

    /**
     * Access the Freebase class searching for the given query.
     *
     * @return A JSON object containing the query results to be processed by client-side JavaScript.
     * @throws ParseException
     */
    @DefaultHandler
    public Resolution search() throws ParseException {

        return new StreamingResolution("application/json", Freebase.search(query));
    }

    /**
     * Access the Freebase class and get the value for a given GUID.
     *
     * @return The Freebase value for the given GUID in text format.
     * @throws ParseException
     */
    public Resolution get() throws ParseException {

        return new StreamingResolution("text", Freebase.get(query));
    }
}
